home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / xview / guidexv / gcm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-14  |  11.1 KB  |  397 lines

  1. /*
  2.  * This file is a product of Sun Microsystems, Inc. and is provided for
  3.  * unrestricted use provided that this legend is included on all tape
  4.  * media and as a part of the software program in whole or part.  Users
  5.  * may copy or modify this file without charge, but are not authorized to
  6.  * license or distribute it to anyone else except as part of a product
  7.  * or program developed by the user.
  8.  * 
  9.  * THIS FILE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  10.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  11.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  12.  * 
  13.  * This file is provided with no support and without any obligation on the
  14.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  15.  * modification or enhancement.
  16.  * 
  17.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  18.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS FILE
  19.  * OR ANY PART THEREOF.
  20.  * 
  21.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  22.  * or profits or other special, indirect and consequential damages, even
  23.  * if Sun has been advised of the possibility of such damages.
  24.  * 
  25.  * Sun Microsystems, Inc.
  26.  * 2550 Garcia Avenue
  27.  * Mountain View, California  94043
  28.  */
  29.  
  30. #ifndef lint
  31. static char    sccsid[] = "@(#)gcm.c    2.12 91/10/15 Copyright 1989 Sun Microsystems";
  32. #endif
  33.  
  34. /*
  35.  * GUIDE colormap segment support functions.
  36.  */
  37.  
  38. #include <ctype.h>
  39. #include <string.h>
  40. #include <sys/param.h>
  41. #include <xview/xview.h>
  42. #include <xview/cms.h>
  43. #include <X11/Xlib.h>
  44. #include <X11/Xutil.h>
  45. #include <X11/Xatom.h>
  46. #include "gcm.h"
  47.  
  48. /*
  49.  * Array of color names.  The RGB values of these colors are looked up at
  50.  * initialization time and inserted into a colormap segment.
  51.  */
  52. char    *Gcm_colornames[] = {
  53.     "BG1",
  54.     "BG2",
  55.     "BG3",
  56.     "Highlight",
  57.     GUIDE_COLOR_LIST,
  58.     "Background",
  59.     "Foreground",
  60.     NULL
  61. };
  62.  
  63. #define    NCOLORS        ((sizeof (Gcm_colornames) / sizeof (char *)) - 1)
  64. #define OFFSET        (CMS_CONTROL_COLORS + 2)
  65.  
  66. static Xv_singlecolor    Colordata[NCOLORS];
  67.  
  68. static int    Bg_index = NCOLORS-2;
  69. static int    Fg_index = NCOLORS-1;
  70.  
  71. static Cms    create_guide_cms();
  72. static int    default_bg_index();
  73. static int    default_fg_index();
  74. static void    get_rgb_def();
  75.  
  76. /*
  77.  * Return the name of GUIDE's colormap segment.
  78.  */
  79. char           *
  80. gcm_colormap_name()
  81. {
  82.     return "GUIDE CMS";
  83. }
  84.  
  85. /*
  86.  * Initialize the application's colormap segment data.
  87.  */
  88. void
  89. gcm_initialize_colors(win, bg, fg)
  90.     Xv_opaque       win;            /* base window */
  91.     char           *bg;            /* initial bg | NULL */
  92.     char           *fg;            /* initial fg | NULL */
  93. {
  94.     Cms        cms;
  95.     int             new_bg;
  96.     int        new_fg;
  97.     int        depth = (unsigned int)xv_get(win, XV_DEPTH);
  98.     int        visual_class = (int)xv_get(win, XV_VISUAL_CLASS);
  99.     Xv_Screen    screen = (Xv_Screen) xv_get(win, XV_SCREEN);
  100.  
  101.     cms = (Cms) xv_find(screen, CMS,
  102.         CMS_NAME,    gcm_colormap_name(),
  103.         XV_AUTO_CREATE,    FALSE,
  104.         0);
  105.  
  106.  
  107.     /*
  108.      * Only initialize colors if we have enough depth and are using
  109.      * a color visual.
  110.      */
  111.     if ((depth < 8) || !((visual_class == StaticColor) ||
  112.         (visual_class == PseudoColor) || (visual_class == TrueColor) ||
  113.         (visual_class == DirectColor)))
  114.         return;
  115.  
  116.     if (!cms)
  117.         cms = create_guide_cms(win);
  118.  
  119.     /*
  120.      * If a default background or foreground color isn't specified, we need
  121.      * to use the default from the old cms before installing the new cms.
  122.      */
  123.     if (!bg || ((new_bg = gcm_color_index(bg)) == -1))
  124.         new_bg = default_bg_index(win);
  125.  
  126.     if (!fg || ((new_fg = gcm_color_index(fg)) == -1))
  127.         new_fg = default_fg_index(win);
  128.  
  129.     /*
  130.      * Install the new cms data.
  131.      */
  132.     xv_set(win,
  133.         WIN_CMS_NAME,    gcm_colormap_name(),
  134.         WIN_CMS,    cms,
  135.         0);
  136.     
  137.     xv_set(win, WIN_BACKGROUND_COLOR, new_bg, 0);
  138.     xv_set(win, WIN_FOREGROUND_COLOR, new_fg, 0);
  139. }
  140.  
  141. /*
  142.  * Return the color index for the specified color.  Returns -1 if the color
  143.  * is not found.
  144.  */
  145. int
  146. gcm_color_index(name)
  147.     char           *name;
  148. {
  149.     register int    i;
  150.     static int    strcasecmp();
  151.  
  152.     if (name && (*name != '\0')) {
  153.         for (i = 0; Gcm_colornames[i]; i++)
  154.             if (strcasecmp(Gcm_colornames[i], name) == 0)
  155.                 return i;
  156.     }
  157.     return -1;
  158. }
  159.  
  160. /*
  161.  * Create a new CMS for a window.  The resulting CMS will be an OPEN LOOK
  162.  * CONTROL_CMS with four slots containing the necessary colors for 3D.
  163.  * CMS is layed out as follows:
  164.  *
  165.  * 0-3            OPEN LOOK CONTROL_CMS colors
  166.  * 4-(NCOLORS-3)    GUIDE Colors from X11R3 database
  167.  * NCOLORS-2        Background from initial frame
  168.  * NCOLORS-1        Foreground from initial frame
  169.  */
  170. static Cms
  171. create_guide_cms(win)
  172.     Xv_opaque    win;
  173. {
  174.     int        i;
  175.     Cms        cms;
  176.     XColor        db_val;
  177.     XColor        hw_val;
  178.     Xv_cmsdata    *ocms_data;
  179.     Xv_opaque    frame = xv_get(win, WIN_FRAME);
  180.     Display        *display = (Display *) XV_DISPLAY_FROM_WINDOW(win);
  181.     Colormap    cmap = xv_get(xv_get(frame, WIN_CMS), CMS_CMAP_ID);
  182.  
  183.     /*
  184.      * Create a new GUIDE CMS.
  185.      */
  186.     for (i = 0; i < NCOLORS - OFFSET; i++) {
  187.         XLookupColor(display, cmap, Gcm_colornames[i+CMS_CONTROL_COLORS],
  188.                  &db_val, &hw_val);
  189.  
  190.         Colordata[i].red = (hw_val.red >> 8);
  191.         Colordata[i].green = (hw_val.green >> 8);
  192.         Colordata[i].blue = (hw_val.blue >> 8);
  193.     }
  194.  
  195.     ocms_data = (Xv_cmsdata *) xv_get(frame, WIN_CMS_DATA);
  196.  
  197.     i = (int) xv_get(frame, WIN_BACKGROUND_COLOR);
  198.  
  199.     Colordata[Bg_index - CMS_CONTROL_COLORS].red = ocms_data->red[i];
  200.     Colordata[Bg_index - CMS_CONTROL_COLORS].green = ocms_data->green[i];
  201.     Colordata[Bg_index - CMS_CONTROL_COLORS].blue = ocms_data->blue[i];
  202.  
  203.     i = (int) xv_get(frame, WIN_FOREGROUND_COLOR);
  204.  
  205.     Colordata[Fg_index - CMS_CONTROL_COLORS].red = ocms_data->red[i];
  206.     Colordata[Fg_index - CMS_CONTROL_COLORS].green = ocms_data->green[i];
  207.     Colordata[Fg_index - CMS_CONTROL_COLORS].blue = ocms_data->blue[i];
  208.  
  209.     cms = xv_create(xv_get(win, XV_SCREEN), CMS,
  210.         CMS_NAME,        gcm_colormap_name(),
  211.         XV_VISUAL,        (Visual *)xv_get(win, XV_VISUAL),
  212.         CMS_TYPE,        XV_STATIC_CMS,
  213.         CMS_CONTROL_CMS,    TRUE,
  214.         CMS_SIZE,        NCOLORS,
  215.         CMS_COLORS,        Colordata,
  216.         0);
  217.  
  218.     /*
  219.      * Save color data after cms has been created.
  220.      */
  221.     xv_get(cms, CMS_COLORS, Colordata);
  222.  
  223.     return cms;
  224. }
  225.  
  226. /*
  227.  * Figure out the index of the default background color for a window
  228.  * before a new cms is installed.
  229.  */
  230. static int
  231. default_bg_index(win)
  232.     Xv_opaque    win;
  233. {
  234.     int        i;
  235.     Xv_singlecolor    rgb_def;
  236.  
  237.     get_rgb_def(win, (int) xv_get(win, WIN_BACKGROUND_COLOR), &rgb_def);
  238.  
  239.     if ((rgb_def.red == Colordata[0].red) &&
  240.         (rgb_def.green == Colordata[0].green) &&
  241.         (rgb_def.blue == Colordata[0].blue))
  242.         i =  0;
  243.     else if ((rgb_def.red == Colordata[Bg_index].red) &&
  244.         (rgb_def.green == Colordata[Bg_index].green) &&
  245.         (rgb_def.blue == Colordata[Bg_index].blue))
  246.         i =  Bg_index;
  247.     else
  248.         i =  gcm_color_index("White");
  249.  
  250.     return i;
  251. }
  252.  
  253. /*
  254.  * Figure out the index of the default foreground color for a window
  255.  * before a new cms is installed.
  256.  */
  257. static int
  258. default_fg_index(win)
  259.     Xv_opaque    win;
  260. {
  261.     int        i;
  262.     Xv_singlecolor    rgb_def;
  263.  
  264.     get_rgb_def(win, (int) xv_get(win, WIN_FOREGROUND_COLOR), &rgb_def);
  265.  
  266.     if ((rgb_def.red == Colordata[Fg_index].red) &&
  267.         (rgb_def.green == Colordata[Fg_index].green) &&
  268.         (rgb_def.blue == Colordata[Fg_index].blue))
  269.         i =  Fg_index;
  270.     else
  271.         i =  gcm_color_index("Black");
  272.  
  273.     return i;
  274. }
  275.  
  276. /*
  277.  * Get the RGB values for a given color index in the current window cms
  278.  */
  279. static void
  280. get_rgb_def(win, i, color)
  281.     Xv_opaque    win;
  282.     int        i;
  283.     Xv_singlecolor    *color;
  284. {
  285.     Xv_cmsdata    *cms_data;
  286.  
  287.     cms_data = (Xv_cmsdata *) xv_get(win, WIN_CMS_DATA);
  288.  
  289.     color->red = cms_data->red[i];
  290.     color->green = cms_data->green[i];
  291.     color->blue = cms_data->blue[i];
  292. }
  293.  
  294. /*
  295.  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  296.  * Local definition of strcasecmp for binary compability between SunOS 4.0
  297.  * and SunOS 4.1.  We have to include this here because 4.1 before FCS
  298.  * did not contain a compatibility routine for stricmp.
  299.  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  300.  */
  301.  
  302. /*
  303.  * This array is designed for mapping upper and lower case letter
  304.  * together for a case independent comparison.  The mappings are
  305.  * based upon ascii character sequences.
  306.  */
  307. static char charmap[] = {
  308.     '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
  309.     '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
  310.     '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
  311.     '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
  312.     '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
  313.     '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
  314.     '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
  315.     '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
  316.     '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
  317.     '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
  318.     '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
  319.     '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
  320.     '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
  321.     '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
  322.     '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
  323.     '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
  324.     '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
  325.     '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
  326.     '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
  327.     '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
  328.     '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
  329.     '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
  330.     '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
  331.     '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
  332.     '\300', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
  333.     '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
  334.     '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
  335.     '\370', '\371', '\372', '\333', '\334', '\335', '\336', '\337',
  336.     '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
  337.     '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
  338.     '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
  339.     '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
  340. };
  341.  
  342. static int
  343. strcasecmp(s1, s2)
  344.     register char *s1, *s2;
  345. {
  346.     register char *cm = charmap;
  347.  
  348.     while (cm[*s1] == cm[*s2++])
  349.         if (*s1++ == '\0')
  350.             return(0);
  351.     return(cm[*s1] - cm[*--s2]);
  352. }
  353.  
  354. /* This should probably be in gcc.c however, NCOLORS is here 
  355.  * (Maybe it should be global?
  356.  *
  357.  * Create object `color_palette_menu' in the specified instance.
  358.  * NOTE: You must call gcm_initialize_colors to initialize the
  359.  *     colormap for the window (control area, canvas, etc) the
  360.  *    menu will be displayed in.
  361.  *        gcm_initialize_colors(window, NULL, NULL);
  362.  */
  363. Xv_opaque
  364. gcm_color_palette_menu_create()
  365. {
  366.     int color_idx;
  367.     Xv_opaque    menu, item;
  368.     Xv_opaque        color_palette_menu_image;
  369.     static unsigned short    color_palette_menu_bits[] = {
  370. #include "black16.pr"
  371.     };
  372.     
  373.     color_palette_menu_image = xv_create(XV_NULL, SERVER_IMAGE,
  374.                 SERVER_IMAGE_DEPTH, 1,
  375.                 SERVER_IMAGE_BITS, color_palette_menu_bits,
  376.                 XV_WIDTH, 16,
  377.                 XV_HEIGHT, 16,
  378.                 NULL);
  379.  
  380.     menu = xv_create(XV_NULL, MENU_CHOICE_MENU,
  381.                 MENU_NCOLS, 9,
  382.                 MENU_COL_MAJOR, FALSE,
  383.                 NULL);
  384.  
  385.     for(color_idx = 0; color_idx < NCOLORS; color_idx++)
  386.     {
  387.         item = menu_create_item(
  388.                 MENU_IMAGE, color_palette_menu_image,
  389.                 MENU_COLOR, color_idx,
  390.                 NULL);
  391.         xv_set(menu, MENU_APPEND_ITEM, item, NULL);
  392.     }
  393.  
  394.     return menu;
  395. }
  396.  
  397.